import * as echarts from '../../../ec-canvas/echarts';

// chart为图表实例，记得要声明为全局的
var chart = null

function initChart(canvas, width, height, dpr) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr
    });
    chart.showLoading() //显示Loading
    canvas.setChart(chart);

    var option = {
        title: {
            text: '获取数据中',
            left: 'center'
        },
    };

    chart.setOption(option);
    return chart;

}

Page({
    data: {
        ec: {
            onInit: initChart
        }
    },
    onLoad() {},

    getData() {
        //这里是模拟的数据请求，项目中请使用wx.request替换掉setTimeouot
        var that=this;
        wx.request({
          url: 'http://127.0.0.1:8080/api/test/getdata2',
          method: 'Post',
          success:function(res){
            let resultdata=res.data.data.genChart //图表数据
            let resultcode=res.data.code //code的值
            if(resultcode==0){
              chart.hideLoading() //隐藏Loading
              chart.setOption({     
                  title: {
                      text: '康复训练数据',
                  },
                  tooltip: {
                      show: true,
                      trigger: 'axis',
                      
                  },
                  xAxis: {
                      type: 'category',
                      data: resultdata.xAxis.data,
                      // show: false
                  },
                  yAxis: {
                      x: 'center',
                      type: 'value',
                       splitLine: {
                         lineStyle: {
                              type: 'dashed'
                          }
                       }
                      // show: false
                  },
                  series: [{
                      name: 'A',
                      type: 'line',
                      smooth: true,
                      data: resultdata.series[0].data
                  }]
              })
            }
          } 
        });
    },
    onReady() {
        //获取数据
        this.getData()
    },
});
